//
//  ViewController.m
//  UI处理复杂数据
//
//  Created by dllo on 16/8/10.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#import "FindCar.h"
#import "Brand.h"
#import "Brandlist.h"
#import "Result.h"
#import "AllCarTableViewCell.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *carTableView;
@property (nonatomic, strong) FindCar *findCar;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self handleData];
}

- (void)handleData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"AllCar" ofType:@"json"];
    NSData *allCarData = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    NSDictionary *allCarDic = [NSJSONSerialization JSONObjectWithData:allCarData options:NSJSONReadingMutableContainers error:&error];
    self.findCar = [FindCar modelWithDic:allCarDic];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.findCar.result.brandlist.count;
}
// 设置分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.findCar.result.brandlist[section].letter;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.findCar.result.brandlist[section].list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AllCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    Brand *brand = self.findCar.result.brandlist[indexPath.section].list[indexPath.row];
    cell.carNameLabel.text = brand.name;
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
